package board;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel; //for creating gui

import ai.AILogic;

public class OldBoard extends JFrame {

	int[][] board; 
	int count; // this makes sure we get vary between setting X and 0 to the screen
	int boardWidth; //used in the AILogic class
	JPanel[][] visualBoard;
	JLabel player;
	JLabel pc;
	AILogic logic;

	// This constructor creates a new (n x n) board
	public OldBoard(int n) {
		boardWidth = n;
		board = new int[n][n];
		
		for (int i = 0; i < board.length; i++) {
			for (int y = 0; y < board.length; y++) {
				board[i][y] = -1; // -1 == empty field
			}
		}
		
		printBoard();

		pack();
		this.setSize(500, 500);
		
		logic = new AILogic(this, true); //player starts
	}
	
	// returns the board
	public int[][] getBoard() {
		return board;
	}

	// sets the indicated field to the indicated icon (1 or 0 or -1)
	public void setBoard(int x, int y, int icon) { 
		board[y][x] = icon;
	}

	//creates the JPanel
	public void printBoard() {
		
		player = new JLabel(new ImageIcon("/Users/Jorgenkg/Documents/Skole/JAVA/ECLIPSE/gomoku/src/board/cross_30x30.png"));
		pc = new JLabel(new ImageIcon("/Users/Jorgenkg/Documents/Skole/JAVA/ECLIPSE/gomoku/src/board/circle_20x25.png"));
		
		
		JPanel holder = new JPanel();
		holder.setLayout(new GridLayout(board.length, board.length));
		MouseAction action = new MouseAction();
		
		for (int y = 0; y < board.length; y++) {
			for (int x = 0; x < board.length; x++) {
				board[y][x]=-1;
//				visualBoard[y][x] = new JPanel();
//				visualBoard[y][x].setOpaque(true);
//				visualBoard[y][x].setBorder(BorderFactory.createLineBorder(Color.black));
//				visualBoard[y][x].addMouseListener(action);
//				holder.add(visualBoard[y][x]); // adds the field to a holder
			}
		}

		add(holder); // adds the holder to the screen
	}

	public int getBoardWidth(){
		return boardWidth;
	}
	
	private void pcTurn() {
		logic.doMove(this);
	}
	
	private void repaintBoard(){
		for(int y=0;y<board.length;y++){
			for(int x=0;x<board.length;x++){
				if(board[y][x]!=-1){
					if(board[y][x]==1) visualBoard[y][x].add(player);
				}
			}
		}
	}
	
	private class MouseAction implements MouseListener {
		
		//What to do when a field is clicked
		private void clicked(JPanel panel){
			panel.setBackground(Color.GREEN);
		}

		public void mouseClicked(MouseEvent event) {
			clicked((JPanel) event.getSource());
			pcTurn();
		}

		public void mouseEntered(MouseEvent event) {}

		public void mouseExited(MouseEvent event) {}

		public void mousePressed(MouseEvent event) {}

		public void mouseReleased(MouseEvent event) {}

	}
		
}
